﻿package {
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.text.TextField;

	public class Main extends Sprite 
	{
		//draw shape
		private var _rect:shape_Sprite;
		private var _ell:shape_Sprite;
		private var _line:shape_Sprite;
		private var _paper:shape_Sprite;
		private var b_x:Number;
		private var b_y:Number;
		private var _w:Number;
		private var _h:Number;
		private var paper:Sprite;
		
		//set interface
		private var i_bg:shape_Sprite;
		private var s_rect:shape_Sprite;
		private var s_ell:shape_Sprite;
		private var s_line:shape_Sprite;
		private var s_select:shape_Sprite;
		//color
		private var c_red:shape_Sprite;
		private var c_pink:shape_Sprite;
		private var c_yellow:shape_Sprite;
		private var c_green:shape_Sprite;
		private var c_blue:shape_Sprite;
		private var c_cyanblue:shape_Sprite;
		private var c_black:shape_Sprite;
		private var color:uint=0x000000;
		private var i_skin:Sprite;
		
		//clean sign
		private var s_cle:shape_Sprite;
		private var l_cle:TextField;
		
		//undo sign
		private var s_undo:Sprite;
		private var l_undo:TextField;
		private var num_undo:int;
		
		public function Main():void 
		{
			init();
			Interface();
			num_undo = _paper.numChildren;
			
			//set skin
			i_skin = new Sprite;
			addChild(i_skin);
			i_skin.addChild(i_bg);
			i_skin.addChild(s_select);
			i_skin.addChild(s_rect);
			i_skin.addChild(s_ell);
			i_skin.addChild(s_line);
			//set color
			i_skin.addChild(c_red);
			i_skin.addChild(c_pink);
			i_skin.addChild(c_yellow);
			i_skin.addChild(c_green);
			i_skin.addChild(c_cyanblue);
			i_skin.addChild(c_blue);
			i_skin.addChild(c_black);
			//set cle sign
			i_skin.addChild(s_cle);
			s_cle.addChild(l_cle);
			s_cle.mouseChildren = false;
			//set undo sign
			s_undo.addChild(l_undo);
			i_skin.addChild(s_undo);
			s_undo.mouseChildren = false;
			
			s_ell.addEventListener(MouseEvent.CLICK,select_ell);
			s_rect.addEventListener(MouseEvent.CLICK,select_rect);
			s_line.addEventListener(MouseEvent.CLICK,select_line);
			s_cle.addEventListener(MouseEvent.CLICK,select_cle);
			s_undo.addEventListener(MouseEvent.CLICK,select_undo);
			
			//select color
			c_red.addEventListener(MouseEvent.CLICK,select_red);
			c_pink.addEventListener(MouseEvent.CLICK,select_pink);
			c_yellow.addEventListener(MouseEvent.CLICK,select_yellow);
			c_green.addEventListener(MouseEvent.CLICK,select_green);
			c_cyanblue.addEventListener(MouseEvent.CLICK,select_cyanblue);
			c_blue.addEventListener(MouseEvent.CLICK,select_blue);
			c_black.addEventListener(MouseEvent.CLICK,select_black);
			
			
		}
		private function init():void
		{
			this.stage.align = StageAlign.TOP_LEFT;
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			this.stage.frameRate = 24;
			this.stage.stageWidth = 800;
			this.stage.stageHeight = 600;
			
			paper = new Sprite;
			_paper= new shape_Sprite;
			_paper.RectSprite(0xFFFFFF,0,60,800,540);
			paper.addChild(_paper);
			addChild(paper);
			
			_paper.addEventListener(MouseEvent.MOUSE_DOWN,begin_rect);
			_paper.addEventListener(MouseEvent.MOUSE_UP,end_rect);
			
			//this.stage.addEventListener(MouseEvent.MOUSE_DOWN,begin_rect);
			//this.stage.addEventListener(MouseEvent.MOUSE_UP,end_rect);
		}
		
		//set Interface
		private function Interface ():void
		{
			//set sign
			i_bg = new shape_Sprite;
			i_bg.RectSprite(0x666666,0,0,800,60);			
			
			s_rect = new shape_Sprite;
			s_rect.RectSprite(0xFFFFFF,648,20,25,20);
			
			s_ell = new shape_Sprite;
			s_ell.EllSprite(0xFFFFFF,695,20,30,20);
			
			s_line = new shape_Sprite;
			s_line.LineSprite(5,0xFFFFFF,750,20,770,40);
			
			s_select = new shape_Sprite;
			//s_select.RectSprite(0xAAAAAA,0,0,40,40); // select
			//s_select.RectSprite(0xAAAAAA,740,10,40,40); // line
			//s_select.RectSprite(0xAAAAAA,690,10,40,40); // ell
			s_select.RectSprite(0xAAAAAA,640,10,40,40); // rec
			
			//color sign
			c_red = new shape_Sprite;
			c_red.RectSprite(0xFF0000,20,10,40,40);	
			
			c_pink = new shape_Sprite;
			c_pink.RectSprite(0xFF00FF,80,10,40,40);	
			
			c_yellow = new shape_Sprite;
			c_yellow.RectSprite(0xFFFF00,140,10,40,40);	
			
			c_green = new shape_Sprite;
			c_green.RectSprite(0x00FF00,200,10,40,40);	
			
			c_cyanblue = new shape_Sprite;
			c_cyanblue.RectSprite(0x00FFFF,260,10,40,40);	
			
			c_blue = new shape_Sprite;
			c_blue.RectSprite(0x0000FF,320,10,40,40);	
			
			c_black = new shape_Sprite;
			c_black.RectSprite(0x000000,380,10,40,40);	
			
			//clean sign
			s_cle = new shape_Sprite;
			s_cle.RectSprite(0xFFFFFF,550,7,35,44);
			l_cle = new TextField;
			l_cle.text="New";
			l_cle.x=553;
			l_cle.y=20;
			
			//undo sign
			l_undo = new TextField;
			s_undo = new Sprite;
			l_undo.text = "←";
			l_undo.x = 480;
			l_undo.y = -35;
			l_undo.scaleX = 0.5;
			l_undo.scaleY = 8;
			l_undo.textColor = 0xFFFFFF;
		}
		private function select_cle (e:MouseEvent):void
		{
			paper.removeChild(_paper);
			_paper = new shape_Sprite;
			_paper.RectSprite(0xFFFFFF,0,60,800,540);
			paper.addChild(_paper);
			
			_paper.addEventListener(MouseEvent.MOUSE_DOWN,begin_rect);
			_paper.addEventListener(MouseEvent.MOUSE_UP,end_rect);
			trace(_paper.numChildren);
		}
		private function select_undo (e:MouseEvent):void
		{			
			if (num_undo > -1)
			{
				_paper.removeChildAt(num_undo);
				num_undo = _paper.numChildren-1;
			}			
			//trace(+_paper.numChildren);
		}
		
		private function select_rect (e:MouseEvent):void
		{
			s_select.x=0;
			//Listener
			_paper.addEventListener(MouseEvent.MOUSE_DOWN,begin_rect);
			_paper.addEventListener(MouseEvent.MOUSE_UP,end_rect);
			//Remove
			_paper.removeEventListener(MouseEvent.MOUSE_DOWN,begin_ell);
			_paper.removeEventListener(MouseEvent.MOUSE_UP,end_ell);
			_paper.removeEventListener(MouseEvent.MOUSE_DOWN,begin_line);
			_paper.removeEventListener(MouseEvent.MOUSE_UP,end_line);
		}
		private function select_ell (e:MouseEvent):void
		{
			s_select.x=50;
			//Listener
			_paper.addEventListener(MouseEvent.MOUSE_DOWN,begin_ell);
			_paper.addEventListener(MouseEvent.MOUSE_UP,end_ell);
			//Remove 
			_paper.removeEventListener(MouseEvent.MOUSE_DOWN,begin_rect);
			_paper.removeEventListener(MouseEvent.MOUSE_UP,end_rect);
			_paper.removeEventListener(MouseEvent.MOUSE_DOWN,begin_line);
			_paper.removeEventListener(MouseEvent.MOUSE_UP,end_line);
		}
		private function select_line (e:MouseEvent):void
		{
			s_select.x=100;
			//Listener
			_paper.addEventListener(MouseEvent.MOUSE_DOWN,begin_line);
			_paper.addEventListener(MouseEvent.MOUSE_UP,end_line);
			//Remove 
			_paper.removeEventListener(MouseEvent.MOUSE_DOWN,begin_rect);
			_paper.removeEventListener(MouseEvent.MOUSE_UP,end_rect);
			_paper.removeEventListener(MouseEvent.MOUSE_DOWN,begin_ell);
			_paper.removeEventListener(MouseEvent.MOUSE_UP,end_ell);
		}
		//color value
		private function select_red (e:MouseEvent):void
		{
			color=0xFF0000;
		}
		private function select_pink (e:MouseEvent):void
		{
			color=0xFF00FF;
		}
		private function select_yellow (e:MouseEvent):void
		{
			color=0xFFFF00;
		}
		private function select_green (e:MouseEvent):void
		{
			color=0x00FF00;
		}
		private function select_cyanblue (e:MouseEvent):void
		{
			color=0x00FFFF;
		}
		private function select_blue (e:MouseEvent):void
		{
			color=0x0000FF;
		}
		private function select_black (e:MouseEvent):void
		{
			color=0x000000;
		}
		
		//draw rectangle
		private function begin_rect (e:MouseEvent):void
		{
			b_x = e.localX;
			b_y = e.localY;
			_rect = new shape_Sprite;

			_paper.addEventListener(Event.ENTER_FRAME,d_rect);
			//trace("X: "+b_x+"\tY: "+b_y);
		}
		private function end_rect (e:MouseEvent):void
		{
			_paper.addChild(_rect);
			_paper.removeEventListener(Event.ENTER_FRAME,d_rect);
			
			num_undo = _paper.numChildren-1;
			//trace(num_undo)
			trace(_paper.getChildIndex(_rect));
			//trace("W: "+_w+"\tH: "+_h);
			//trace(paper.numChildren);
		}
		private function begin_ell (e:MouseEvent):void
		{
			b_x = e.localX;
			b_y = e.localY;
			_ell =new shape_Sprite;

			_paper.addEventListener(Event.ENTER_FRAME,d_ell);
		}
		private function end_ell (e:MouseEvent):void
		{
			_paper.addChild(_ell);
			_paper.removeEventListener(Event.ENTER_FRAME,d_ell);
			num_undo = _paper.numChildren-1;
			trace(_paper.getChildIndex(_ell));
		}
		private function begin_line (e:MouseEvent):void
		{
			b_x = e.localX;
			b_y = e.localY;
			_line =new shape_Sprite;
			_paper.addEventListener(Event.ENTER_FRAME,d_line);
		}
		private function end_line (e:MouseEvent):void
		{
			_paper.addChild(_line);
			_paper.removeEventListener(Event.ENTER_FRAME,d_line);
			num_undo = _paper.numChildren-1;
			trace(_paper.getChildIndex(_line));
		}
		
		//draw rectangle
		private function d_rect (e:Event):void
		{
			_w = mouseX-b_x;
			_h = mouseY-b_y;

			_rect.graphics.clear();
			_rect.RectSprite(color,b_x,b_y,_w,_h);			
			addChild(_rect);
		}
		
		//draw circle
		private function d_ell (e:Event):void
		{
			//_r = Math.sqrt((mouseX-b_x)*(mouseX-b_x)+(mouseY-b_y)*(mouseY-b_y));
			_w = mouseX-b_x;
			_h = mouseY-b_y;
			
			_ell.graphics.clear();
			_ell.EllSprite(color,b_x,b_y,_w,_h);
			addChild(_ell);
		}
		
		//draw line
		private function d_line (e:Event):void
		{
			_line.graphics.clear();
			_line.LineSprite(1,color,b_x,b_y,mouseX,mouseY);
			addChild(_line);
		}
	}
}